package ch06;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

/**
 * @author 崔书健
 * @date 2024/7/10
 */
@Aspect
public class UserServiceLogger {

    private static Logger logger = LogManager.getLogger(UserServiceLogger.class);


    @Before("execution(* UserService.*(..))")
    public void before(JoinPoint jp){
        logger.info("【前置增强】调用 " + jp.getTarget() + " 的 " + jp.getSignature().getName() + "方法。方法入参：" + Arrays.toString(jp.getArgs()));
    }

    @AfterReturning(pointcut = "execution(* UserService.*(..))",returning = "result")
    public void afterRetuming(JoinPoint jp,Object result){
        logger.info("【后置增强】调用 " + jp.getTarget() + " 的 " + jp.getSignature().getName() + " 方法。方法返回值：" + result);
    }

    @AfterThrowing(pointcut = "execution(* UserService.*(..))",throwing = "e")
    public void afterThrowing(JoinPoint jp,RuntimeException e){
        logger.error("【异常增强】" + jp.getSignature().getName() + " 方法发生异常：" + e);
    }

    @After("execution(* UserService.*(..))")
    public void afterLogger(JoinPoint jp){
        logger.info("【最终增强】" + jp.getSignature().getName() + " 方法结束执行");
    }
}
